package com.yxy.service.impl;

import com.yxy.mapper.VisitMapper;
import com.yxy.pojo.entity.User;
import com.yxy.pojo.entity.Visit;
import com.yxy.pojo.vo.VisitVO;
import com.yxy.service.VisitService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.util.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Slf4j
public class VisitServiceImpl implements VisitService {

    @Autowired
    private VisitMapper visitMapper;

//    /**
//     * 添加访问记录
//     *
//     * @param user
//     */
//    @Override
//    public void add(User user) {
//        Visit visit = Visit.builder()
//                .userId(user.getId())
//                .visitTime(LocalDateTime.now())
//                .build();
//        visitMapper.add(visit);
//    }

    /**
     * 添加访问记录
     *
     * @param userId
     */
    public void add(int userId) {
        // 先查询最近的访问时间
        List<Visit> visitList = visitMapper.selectByUserId(userId);
        if (visitList.size() > 0) {
            Visit visit = visitList.get(visitList.size() - 1);
            LocalDate lastVisitTime = visit.getVisitTime().toLocalDate();
            if (lastVisitTime.equals(LocalDate.now())) {
                // 如果是同一天，则不添加访问记录
                log.info("同一天访问，不添加访问记录");
                return;
            }
            visitMapper.add(Visit.builder()
                    .userId(userId)
                    .visitTime(LocalDateTime.now())
                    .build());
        } else {
            visitMapper.add(Visit.builder()
                    .userId(userId)
                    .visitTime(LocalDateTime.now())
                    .build());
        }
    }

    /**
     * 获取访问记录
     *
     * @return
     */
    @Override
    public VisitVO getVisit() {
        // 总访问量
        int totalVisit = visitMapper.getTotalVisit();
        // 昨日访问量
        int yesterdayVisit = visitMapper.getYesterdayVisit();
        // 上周访问量
        int lastWeekVisit = visitMapper.getLastWeekVisit();
        return VisitVO.builder()
                .totalVisit(totalVisit)
                .yesterdayVisit(yesterdayVisit)
                .lastWeekVisit(lastWeekVisit)
                .build();
    }

    /**
     * 获取系统访问数据
     * @return
     */
    @Override
    public Pair<List<String>, List<Integer>> getSystemVisitData() {
        List<String> dates = new ArrayList<>();
        // 根据当前时间获取前七天的时间
        for (int i = 0; i < 7; i++) {
            LocalDate localDate = LocalDate.now().minusDays(i);
            dates.add(localDate.toString());
        }
        // 反转集合中的数据
        dates.sort((o1, o2) -> o1.compareTo(o2));
        // 查询七天内每天内每天的访问量
        List<Integer> visitCounts = new ArrayList<>();
        for (String date : dates) {
            int visitCount = visitMapper.getVisitCount(date);
            visitCounts.add(visitCount);
        }
        return Pair.create(dates, visitCounts);
    }
}
